package ldkxingzhe.androidmedia.chapter3;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.ImageView;

import java.io.FileNotFoundException;

import ldkxingzhe.androidmedia.R;

/**
 * Created by ke on 15-10-27.
 */
public class ImageEditAndProcessActivity extends AppCompatActivity implements View.OnClickListener {
    private final String TAG = ImageEditAndProcessActivity.class.getSimpleName();

    // MARK: UI Elements
    private ImageView originImage;
    private ImageView alertedImage;

    // MARK: Constants
    private final int REQUEST_CODE_PICK_PICTURE = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chapter3_image_edit);
        originImage = (ImageView)findViewById(R.id.chapter3_origin_picture);
        alertedImage = (ImageView)findViewById(R.id.chapter3_alerted_picture);
        findViewById(R.id.chapter3_choose_picture).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Log.v(TAG, "onClick, choose picture");
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, REQUEST_CODE_PICK_PICTURE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REQUEST_CODE_PICK_PICTURE){
            if(resultCode == RESULT_OK) {
                Log.v(TAG, "pick picture, result come back");
                Uri imageFileUri = data.getData();
                Display currentDisplay = getWindowManager().getDefaultDisplay();
                Point disPlaySize = new Point();
                currentDisplay.getSize(disPlaySize);
                int dw = disPlaySize.x;
                int dh = disPlaySize.y/2 - 100;

                Log.v(TAG,"dw is " + dw + ", and dh is " + dh);

                BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                bitmapOptions.inJustDecodeBounds = true;
                try {
                    BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bitmapOptions);
                    Log.v(TAG,"outWidth is " + bitmapOptions.outWidth);
                    int widthRatio = (int)Math.ceil(bitmapOptions.outWidth/(float)dw);
                    int heightRatio = (int)Math.ceil(bitmapOptions.outHeight/(float)dh);
                    int ratio = 1;
                    if(widthRatio > 1 && heightRatio > 1){
                        ratio = Math.max(widthRatio, heightRatio);
                    }
                    bitmapOptions.inJustDecodeBounds = false;
                    Log.v(TAG,"ratio is " + ++ratio);
                    bitmapOptions.inSampleSize = ratio;
                    Bitmap originBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri),
                            null, bitmapOptions);
                    Log.v(TAG,"bitmap's width is " + originBitmap.getWidth() + ", and bitmap's height is " + originBitmap.getHeight());
                    originImage.setImageBitmap(originBitmap);
                    dealWithBitmap(originBitmap);
                } catch (FileNotFoundException e) {
                    Log.e(TAG,e.getMessage(),e);
                }
            }
        }
    }

    private void dealWithBitmap(final Bitmap bitmap){
        Log.d(TAG,"dealWithBitmap");
        Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
        Canvas canvas = new Canvas(newBitmap);
        Paint paint = new Paint();

        Matrix matrix = new Matrix();
        matrix.setScale(0.5f,0.8f);
        matrix.preTranslate(bitmap.getWidth()/2, 50);
        canvas.drawBitmap(bitmap, matrix, paint);
        alertedImage.setImageBitmap(newBitmap);
    }
}
